package com.example.monitor.service.impl;

import com.example.monitor.model.DTO.WorkshopRankQueryDTO;
import com.example.monitor.model.VO.ResultVO;
import com.example.monitor.model.VO.WorkshopRankVO;
import com.example.monitor.repository.AlarmRecordRepository;
import com.example.monitor.service.WorkshopRankService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class WorkshopRankServiceImpl implements WorkshopRankService {

    private final AlarmRecordRepository alarmRecordRepository;

    @Override
    public ResultVO<List<WorkshopRankVO>> getWorkshopRank(WorkshopRankQueryDTO queryDTO) {
        // 解析时间范围
        Timestamp startTime = parseTimeRange(queryDTO.getTimeRange());

        // 查询数据
        List<WorkshopRankVO> rankList = alarmRecordRepository.findWorkshopAlarmRank(
                queryDTO.getWorkshop(),
                queryDTO.getAlarmType(),
                startTime);

        return ResultVO.success("查询成功", rankList);
    }

    private Timestamp parseTimeRange(String timeRange) {
        if (timeRange == null) {
            return Timestamp.valueOf(LocalDateTime.now().minusDays(7));
        }
        switch (timeRange) {
            case "1天": return Timestamp.valueOf(LocalDateTime.now().minusDays(1));
            case "1周": return Timestamp.valueOf(LocalDateTime.now().minusWeeks(1));
            case "1月": return Timestamp.valueOf(LocalDateTime.now().minusMonths(1));
            default: return Timestamp.valueOf(LocalDateTime.now().minusDays(7));
        }
    }
}
